package com.cjst.controller.api.part;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjst.controller.base.BaseController;
import com.cjst.pojo.part.PartType;
import com.cjst.pojo.part.Parts;
import com.cjst.pojo.part.PtypeRelevance;
import com.cjst.sdp.util.MapUtils;
import com.cjst.service.part.PartTypeService;
import com.cjst.service.part.PartsService;
import com.cjst.service.part.PtypeRelevanceService;
import com.cjst.sql.C;
import com.cjst.sql.Method;
import com.cjst.sql.WherePrams;
import com.cjst.util.common.ResultCode;
import com.cjst.util.json.ResultObj;


@Controller
@RequestMapping("/api/parts")
public class PartsController extends BaseController{
	protected final Logger log = Logger.getLogger(PartsController.class);
	@Autowired
	private PartTypeService partTypeService;
	@Autowired
	private PtypeRelevanceService ptypeRelevanceService;
	@Autowired
	private PartsService partsService;
	
	
	@Value("${file.path}")
	private String begin_url;
	/**
	 * 获取部件类别
	 * @return
	 */
	@RequestMapping(value="/partType",method = RequestMethod.POST)
	@ResponseBody
	public String partType(String partTypeId){
		try {
			WherePrams wheres=Method.where("delFlag", C.EQ, 0);
			//wheres.and("ptypeParent", C.EQ, "0");
			wheres.and("isApp", C.EQ, 1);
			if(partTypeId!=null && partTypeId.length()>0){
				wheres.and("ptypeParent", C.EQ, partTypeId);
			}else{
				wheres.and("ptypeParent", C.EQ, "0");
			}
			//wheres.and("typePartition", C.EQ, 2);
			wheres.orderBy(" px asc");
			List<PartType> ptList=partTypeService.list(wheres);
			/*
			 * ptList.forEach(tmp->{ tmp.setFback(begin_url+tmp.getFback());
			 * 
			 * });
			 */
			if(ptList!=null && ptList.size()>0){
				for (PartType partType : ptList) {
					if(partTypeId==null || partTypeId.length()==0){
						WherePrams isWheres=Method.where("delFlag", C.EQ, 0);
						isWheres.and("ptypeParent", C.EQ, partType.getId());
						int count=(int) partTypeService.count(isWheres);
						if(count>1){
							partType.setIsParts(1);
						}else{
							partType.setIsParts(0);
						}
					}
					partType.setFback(begin_url+partType.getFback());
				}
			}
			ResultObj robj = new ResultObj(ResultCode.RESULT_SUCCESS, "success",ptList);
			return robj.toString();
		}catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			ResultObj robj = new ResultObj(ResultCode.RESULT_EXCEPTION, "网络异常",null);
			return robj.toString();
		}
	}
	
	/**
	 * 获取部件类别
	 * @return
	 */
	@RequestMapping(value="/getPartsByType",method = RequestMethod.POST)
	@ResponseBody
	public String getPartsByType(String partTypeId,String areaId){
		try {
			HashMap<String, String> map=new  HashMap<String, String>();
			WherePrams partWhere=Method.where("delFlag", C.EQ, 0);
			WherePrams wheres=Method.where("ptypeParent", C.EQ, partTypeId);
			
			if(areaId!=null&&areaId.length()>0) {
				wheres.and("areaId", C.EQ, areaId);
			}
			
			
			
			wheres.and("isApp", C.EQ, 1);
			List<PartType> ptList=partTypeService.list(wheres);
			if(ptList!=null && ptList.size()>0){
				List<String> list=new ArrayList<String>();
				for (PartType partType : ptList) {
					map.put(partType.getId(), partType.getFback());
					list.add(partType.getId());
				}
				WherePrams relevanceWhere=Method.where("delFlag", C.EQ, 0);
				relevanceWhere.andIn("typeId", C.IN, list);
				List<PtypeRelevance> relevanceList=ptypeRelevanceService.list(relevanceWhere);
				if(relevanceList!=null && relevanceList.size()>0){
					List<String> pList=new ArrayList<String>();
					for (PtypeRelevance ptypeRelevance : relevanceList) {
						map.put(ptypeRelevance.getPartId(), map.get(ptypeRelevance.getTypeId()));
						pList.add(ptypeRelevance.getPartId());
					}
					partWhere.andIn("id", C.IN, pList);
				}else{ 
					partWhere.and("id", C.EQ, "-1");
				}
			}else{
				PartType partType=partTypeService.get(partTypeId);
				if(partType!=null){
					WherePrams relevanceWhere2=Method.where("delFlag", C.EQ, 0);
					relevanceWhere2.and("typeId", C.EQ, partTypeId);
					List<PtypeRelevance> relevanceList2=ptypeRelevanceService.list(relevanceWhere2);
					if(relevanceList2!=null && relevanceList2.size()>0){
						List<String> pList2=new ArrayList<String>();
						for (PtypeRelevance ptypeRelevance2 : relevanceList2) {
							map.put(ptypeRelevance2.getPartId(), partType.getFback());
							pList2.add(ptypeRelevance2.getPartId());
						}
						partWhere.andIn("id", C.IN, pList2);
					}else{
						partWhere.and("id", C.EQ, "-1");
					}
				}
				
			}
			if(areaId!=null && areaId.length()!=0){
				partWhere.and("areaId", C.EQ, areaId);
			}
			List<Parts> partsList=partsService.list(partWhere);
			List<Map<String, Object>> partsListMap=new ArrayList<Map<String, Object>>();
			if(partsList!=null && partsList.size()>0){
				for (Parts parts : partsList) {
					parts.setFback1(begin_url+parts.getFback1());
					parts.setTbImg(begin_url+map.get(parts.getId()));
					Map<String, Object> partsMap = MapUtils.transBean2Map(parts);
					List<String> strList=new ArrayList<String>();
					if(parts.getFback2()!=null && parts.getFback2().length()>0){
						String[] str=parts.getFback2().split(";");
						for(int i=0;i<str.length;i++){
							strList.add(str[i]);
						}
					}
					partsMap.put("strList", strList);
					partsListMap.add(partsMap);
				}
			}
			
			/*partsList.forEach(tmp->{
				tmp.setFback1(begin_url+tmp.getFback1());
				
			});*/
			
			
			ResultObj robj = new ResultObj(ResultCode.RESULT_SUCCESS, "success",partsListMap);
			return robj.toString();
		}catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			ResultObj robj = new ResultObj(ResultCode.RESULT_EXCEPTION, "网络异常",null);
			return robj.toString();
		}
	}
}






